package org.spring.springboot.exception.common.ascept;

import org.apache.ibatis.exceptions.TooManyResultsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@Component
public class ExceptionHandle {

  private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);

  /**
   * 判断错误是否是已定义的已知错误，不是则由未知错误代替，同时记录在log中
   * @param e
   * @return
   */
  @ExceptionHandler(value = Exception.class)
  @ResponseBody
  public Result<?> exceptionGet(Exception e){
	  
      if(e instanceof StringIndexOutOfBoundsException){
    	  return ResultUtil.error(ExceptionEnum.STRINGINDEXOUTOFBOUNDS);
      }else if(e instanceof NullPointerException){
    	  return ResultUtil.error(ExceptionEnum.NULLPOINTER);
      }else if(e instanceof TooManyResultsException){
    	  return ResultUtil.error(ExceptionEnum.TOOMANYRESULT);
      }

      LOGGER.error("【系统异常】{}",e);
      return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
  }
}